/**
 * 菜单管理JS
 * @author Assassin
 */
//加载treeTable数据
	function selectTreeTableData() {
		shiro.ajax({
 			url: '/menu/treeTable',
 			data:{},
 			success: function(data) {
 				if(data.result == 0) {
 					var treeTableData = '';
 					$.each(data.data, function (index, val) {
						if(val.menuLevel == 1) {
							treeTableData += 
		 	                      '<tr data-tt-id="' + val.menuId + '">'
		 	                    + ' <td style="text-align: center;"><input type="checkbox" value="' + val.menuId + '" name="menuIdCheckBox"  onclick="javascript:dynamicCheck(this);"/></td>'
								+ '	<td>' + shiro.util.formatTableData("-",val.menuLevel) + '</td>'
								+ '	<td>' + shiro.util.formatTableData("-",val.menuName) + '</td>'
								+ ' <td>' + shiro.util.formatTableData("-",val.aliasName)+ '</td>'
								+ ' <td>' + shiro.util.formatTableData("-",val.menuDesc) + '</td>'
								+ ' <td>' + shiro.util.formatTableData("-",val.menuUrl) + '</td>'
		 						+ ' <td style="text-align: center;">'
		 						+ '		<a  class="link-add" title="添加" href="javascript:;" onclick="javascript:addSubMenu(' + val.menuId + ');">'
		 						+ '			添加'
		 						+ '		</a>'
		 						+ '		<a   class="link-update" title="修改" href="javascript:;" onclick="javascript:editMenu(' + val.menuId + ');">'
		 						+ '			修改'
		 						+ '		</a>'
		 						+ '		<a  class="link-del" title="删除" href="javascript:;" onclick="javascript:deleteMenu(' + val.menuId + ',' + val.isParent + ')" >'
		 						+ '			删除'
		 						+ '		</a>'
		 						+ '	</td>'
		 						+ '</tr>';	
		 					if(val.isParent == 1 && val.subList) {
		 						treeTableData += recursionSubMenu(val.subList, val.menuId);
		 					}
						}
 					});
 					$("#treeTableBody").empty();
 					$("#treeTableBody").append(treeTableData);
 					$("#treeTable").treetable(option, true);
 				}
 			}
 		});
	}
	
	/**
	 * 递归添加菜单子集
	 * @param resultList 结果List
	 * @param menuPic 父级菜单Id
	 */
	function recursionSubMenu(subList, menuPid) {
		var result = '';
		$.each(subList, function (index, val) {
			result += 
                 '<tr data-tt-id="' + val.menuId + '" data-tt-parent-id="' + menuPid + '">'
                + ' <td style="text-align: center;"><input type="checkbox" value="' + val.menuId + '" name="menuIdCheckBox" onclick="javascript:dynamicCheck(this);" /></td>'
                + '	<td>' + shiro.util.formatTableData("-",val.menuLevel) + '</td>'
				+ '	<td>' + shiro.util.formatTableData("-",val.menuName) + '</td>'
				+ ' <td>' + shiro.util.formatTableData("-",val.aliasName)+ '</td>'
				+ ' <td>' + shiro.util.formatTableData("-",val.menuDesc) + '</td>'
				+ ' <td>' + shiro.util.formatTableData("-",val.menuUrl) + '</td>'
				+ ' <td style="text-align: center;">'
				+ '		<a  class="link-add" title="添加" href="javascript:;" onclick="javascript:addSubMenu(' + val.menuId + ');">'
				+ '			添加'
				+ '		</a>'
				+ '		<a   class="link-update" title="修改" href="javascript:;" onclick="javascript:editMenu(' + val.menuId + ');">'
				+ '			修改'
				+ '		</a>'
				+ '		<a  class="link-del" title="删除" href="javascript:;" onclick="javascript:deleteMenu(' + val.menuId + ',' + val.isParent + ')" >'
				+ '			删除'
				+ '		</a>'
				+ '	</td>'
				+ '</tr>';	
			if(val.isParent == 1 && val.subList) {
				result += recursionSubMenu(val.subList, val.menuId);
			}
		});
		return result;
	}

	// 动态勾选菜单信息
	function dynamicCheck(obj) {
		var isChecked = $(obj).prop('checked');
		var subArray = new Array();
		recursionSub(obj, subArray);
		for(var i = 0; i < subArray.length; i++) {
			var sid = subArray[i];
			$('#treeTableBody').find("tr").each(function(){
				var menuId = $(this).attr('data-tt-id');
				if(sid == menuId) {
					var tdArr = $(this).children();
					tdArr.eq(0).find('input').prop('checked', isChecked);
				}
			});
		} 
	}
	
	// 递归查找子菜单
	function recursionSub(obj, subArray) {
		var menuId = $(obj).parent().parent().attr('data-tt-id');
		$('#treeTableBody').find("tr").each(function(){
			var subMenuId = $(this).attr('data-tt-id');
			var subMenuPid = $(this).attr('data-tt-parent-id');
			if(subMenuPid == menuId) {
				subArray.push(subMenuId);
				var tdArr = $(this).children();
				recursionSub(tdArr.eq(0).find('input'), subArray);
			}
		});
	}
	
	
	function  addSubMenu(menuId){
		window.location.href=shiro.baseUrl + "/menu/addSubMenu?menuPid="+menuId;
	}
	
	function  editMenu(menuId){
		window.location.href=shiro.baseUrl + "/menu/editMenu?menuId="+menuId;
	}
	

// 添加菜单
function executeMenuAdd() {
    // 表单验证
    var menuName = $.trim($('#menuName').val());
    if(shiro.util.isEmpty(menuName)) {
        layer.tips('菜单名称不能为空', '#menuName');
        return false;
    }
    var menuUrl = $.trim($('#menuUrl').val());
    if(shiro.util.isEmpty(menuUrl)) {
    	layer.tips('菜单路径不能为空', '#menuUrl');
    	return false;
    }
    // 提交添加
    shiro.ajax({
        url: '/menu/executeAddMenu',
        data:$('#menuAddForm').serialize(),
        success: function(data) {
            if(data.result == 0) {
                layer.msg(data.message, {icon: 1,time: 1000}, function() {
                    window.location.href = shiro.baseUrl + "menu/list";
                });
            } else {
                layer.msg(data.message, {icon: 5,time: 1000});
                $('#menuName').val('');
            }
        }
    });
    return false;
}

//添加根菜单
function executeMenuSubAdd() {
	 // 表单验证
    var menuName = $.trim($('#menuName').val());
    if(shiro.util.isEmpty(menuName)) {
        layer.tips('菜单名称不能为空', '#menuName');
        return false;
    }
    var menuUrl = $.trim($('#menuUrl').val());
    if(shiro.util.isEmpty(menuUrl)) {
    	layer.tips('菜单路径不能为空', '#menuUrl');
    	return false;
    }
    // 提交添加
    shiro.ajax({
        url: '/menu/executeAddSubMenu',
        data:$('#menuSubAddForm').serialize(),
        success: function(data) {
            if(data.result == 0) {
                layer.msg(data.message, {icon: 1,time: 1000}, function() {
                    window.location.href = shiro.baseUrl + "menu/list";
                });
            } else {
                layer.msg(data.message, {icon: 5,time: 1000});
                $('#menuName').val('');
            }
        }
    });
    return false;
}

// 修改菜单
function executeMenuEdit() {
	 // 表单验证
    var menuName = $.trim($('#menuName').val());
    if(shiro.util.isEmpty(menuName)) {
        layer.tips('菜单名称不能为空', '#menuName');
        return false;
    }
    var menuUrl = $.trim($('#menuUrl').val());
    if(shiro.util.isEmpty(menuUrl)) {
    	layer.tips('菜单路径不能为空', '#menuUrl');
    	return false;
    }
    // 提交修改
    shiro.ajax({
        url: 'menu/executeEditMenu',
        data:$('#menuEditForm').serialize(),
        success: function(data) {
            if(data.result == 0) {
                layer.msg(data.message, {icon: 1,time: 1000}, function() {
                    window.location.href = shiro.baseUrl + "menu/list";
                });
            } else {
                layer.msg(data.message, {icon: 5,time: 1000});
                $('#menuName').val('');
            }
        }
    });
    return false;
}

//删除菜单信息
function deleteMenu(menuId, isParent) {
	var hitMessage = '确认要删除该菜单？';
	if(isParent == 1) {
		hitMessage = '【系统提示】您想要删除的菜单含有子菜单，系统会同步删除所有子菜单，请谨慎操作，确认删除？';
	}
	layer.confirm(hitMessage, {title:'系统消息'},function(index){
		shiro.ajax({
 			url: 'menu/delete',
 			data:{'menuId' : menuId},
 			success: function(data) {
 				if(data.result == 0) {
 					layer.msg(data.message, {icon: 1,time: 1500}, function() { 
 						window.location.href = shiro.baseUrl + "/menu/list";
 					});
 				} else {
 					layer.msg(data.message, {icon: 5,time: 1500});
 				}
 			}
 		});
	});
}

// 批量删除菜单
function batchDeleteMenu() {
    var menuIdArray = shiro.form.getCheckBoxSelectValues('menuIdCheckBox');
    if(menuIdArray.length == 0) {
        layer.msg('请选择要操作的数据行', {icon: 5,time: 1000});
    } else {
        layer.msg('确定删除选中的菜单？', {
            time: 0,
            btn: ['确定', '取消'],
            yes: function(index){
                layer.close(index);
                var menuIds = menuIdArray.join(',');
                // 提交删除
                shiro.ajax({
                    url: 'menu/batchDelete',
                    data:{"menuIds": menuIds},
                    success: function(data) {
                        if(data.result == 0) {
                            layer.msg(data.message, {icon: 1,time: 1000}, function() {
                                window.location.href = shiro.baseUrl + "menu/list";
                            });
                        } else {
                            layer.msg(data.message, {icon: 5,time: 1000});
                        }
                    }
                });
            }
        });
    }
}